////////////////////////////////////////////////////////////////////////////////
//                                                                            //
// This file is a part of PMCG project. PMCG is the acronym of Physical       //
// Methods in Computer Graphics.                                              //
//                                                                            //
// Classical algorithms in computer graphics use discrete objects in 3D space //
// like vectors, matrices, triangles, planes. Physical way use an  analytical //
// objects like function, scalar and vector fields. Physical way gives new    //
// look to classical algorithms and allows to overcome some difficulties of   //
// pure geometric methods.                                                    //
//                                                                            //
// Project site:                                                              //
//     http://code.google.com/p/pmcg/                                         //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////

#include <cstdlib>
#include <ctime>

#include "MathAlgs.h"
#include "BBox.h"

BBox::BBox()
{
    Init();
}

BBox::BBox(const TriMesh & mesh)
{
    Init();

    TriMesh::Vectors3D::const_iterator it;
    const TriMesh::Vectors3D & vertices = mesh.m_vertices;
    for (it = vertices.begin(); it != vertices.end(); ++it)
    {
        const Vector3D & v = (*it);

        SetMinMax(v.m_x, m_min_x, m_max_x);
        SetMinMax(v.m_y, m_min_y, m_max_y);
        SetMinMax(v.m_z, m_min_z, m_max_z);
    }
}

BBox::~BBox()
{
}

void BBox::Init()
{
    m_min_x = 0;
    m_max_x = 0;

    m_min_y = 0;
    m_max_y = 0;

    m_min_z = 0;
    m_max_z = 0;
}

Vector3D BBox::GetRandomPoint()
{
    srand(time(NULL));

    double x = m_min_x + (m_max_x - m_min_x) * rand() / RAND_MAX;
    double y = m_min_y + (m_max_y - m_min_y) * rand() / RAND_MAX;
    double z = m_min_z + (m_max_z - m_min_z) * rand() / RAND_MAX;

    return Vector3D(x, y, z);
}
